package com.ruc.dbiir.rest.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

public class SSHWindows {


	public static List<String> executeWinCmd(){

//		String hostname ="10.77.40.57";
//		String username="XXT";
//		String password="xiaojian";
		
		String hostname ="192.168.8.2";
		String username="zdyf";
		String password="zdyf";
		List<String> timeList = new ArrayList<String>();
		
		try{
			//建立连接
			Connection conn= new Connection(hostname);
			//     System.out.println("set up connections");
			conn.connect();
			//利用用户名和密码进行授权
			boolean isAuthenticated = conn.authenticateWithPassword(username, password);
			if(isAuthenticated ==false)
			{
				//     	System.out.println("--------");
				throw new IOException("Authorication failed");
			}
			//打开会话
			Session sess = conn.openSession();
			//    System.out.println("cmd----");
			//执行命令
//			sess.execCommand("sqlplus scott/Cheng1jian @E:\\oracle\\Query_Oracle.sql");
			sess.execCommand("sqlplus zdyf/zdyf @C:\\Users\\Administrator\\Desktop\\ruc\\Query_Oracle.sql");
			//     System.out.println("The execute command output is:");
			InputStream stdout = new StreamGobbler(sess.getStdout());
			BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
			while(true)
			{
				String line = br.readLine();
				if(line==null) break;
				System.out.println(line);
				if (line.length()>0) {
					if (line.charAt(0)=='t') {
						timeList.add(line.split("=")[1].trim());
					}
					
				}
			}
			//   System.out.println("Exit code "+sess.getExitStatus());
			sess.close();
			conn.close();
			//     System.out.println("Connection closed");

		}catch(IOException e)
		{
			System.out.println("can not access the remote machine");
		}
//		
//		for(int j=0;j<timeList.size();j++) {
//			System.out.println(timeList.get(j));
//		}
		
		return timeList;

	}


	public static void main(String[] args) {
		executeWinCmd();
	}



}
